package com.zhisheng.gufei.city.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.zhisheng.core.service.impl.BaseServiceImpl;
import com.zhisheng.gufei.city.dao.CityDao;
import com.zhisheng.gufei.city.entity.City;
import com.zhisheng.gufei.city.entity.Node;
import com.zhisheng.gufei.city.service.CityService;


@Service("cityService")
public class CityServiceImpl extends BaseServiceImpl<City,Integer,CityDao> implements CityService {

    public List<City> getAllCity() {
        return this.entityDao.findAll();
    }
    
    public void setCityDao(CityDao cityDao) {
        this.entityDao = cityDao;
    }
   
	public City getOriginCity() {
		return (City) this.entityDao.findByQueryUniqueResult("From City as c where c.id=?", new Integer[]{1});
	}
	
	public List<Node> getCityNodeList(City root) {
		List<Node> result=new ArrayList<Node>();
		
		Set<City> subCity = root.getSubs();
		
		for (City city :  subCity) {
				Node node = new Node();
				node.setId(String.valueOf(city.getId()));
				node.setText(city.getCityName());

				Set<City> sub = city.getSubs();
				if (sub != null && !sub.isEmpty()) {
					node.setLeaf(false);
					node.setChildren(getCityNodeList(city));
				}
				else {
					node.setLeaf(true);
				}
				result.add(node);
	    }
	
		Collections.sort(result);
		return result;
	}
}
